package com.victor.common.constant.factory;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.victor.common.constant.Const;
import com.victor.common.constant.Magic;
import com.victor.common.constant.state.ManagerStatus;
import com.victor.common.constant.state.MenuStatus;
import com.victor.common.constant.state.Sex;
import com.victor.common.persistence.dao.*;
import com.victor.common.persistence.model.*;
import com.victor.core.log.LogObjectHolder;
import com.victor.core.support.StrKit;
import com.victor.core.util.Convert;
import com.victor.core.util.DateUtil;
import com.victor.core.util.SpringContextHolder;
import com.victor.core.util.ToolUtil;
import com.victor.modular.system.dao.AudienceCodeDao;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 常量的生产工厂
 */
@Component
@DependsOn("springContextHolder")
public class ConstantFactory implements IConstantFactory {

    private RoleMapper roleMapper = SpringContextHolder.getBean(RoleMapper.class);
    private UserMapper userMapper = SpringContextHolder.getBean(UserMapper.class);
    private UsersImagesMapper usersImagesMapper = SpringContextHolder.getBean(UsersImagesMapper.class);
    private MenuMapper menuMapper = SpringContextHolder.getBean(MenuMapper.class);
    private LoginLogMapper loginLogMapper = SpringContextHolder.getBean(LoginLogMapper.class);
    private AudienceCodeMapper audienceCodeMapper = SpringContextHolder.getBean(AudienceCodeMapper.class);
    private AudienceCodeDao audienceCodeDao = SpringContextHolder.getBean(AudienceCodeDao.class);
    private WeddingDressMapper weddingDressMapper = SpringContextHolder.getBean(WeddingDressMapper.class);

    public static IConstantFactory me() {
        return SpringContextHolder.getBean("constantFactory");
    }
//    public static int count=0;
    @Override
    public String getAudienceName(String code) {
        List<AudienceCode> list = audienceCodeMapper.selectList(new EntityWrapper<AudienceCode>().eq("code", code));
        if(ToolUtil.isNotEmpty(list)){
            return list.get(0).getName();
        }
//        System.out.println(count++);
        return null;
    }

    public String getWeddingDressName(Integer id,Integer type){
        List<WeddingDress> list = weddingDressMapper.selectList(new EntityWrapper<WeddingDress>().eq("id", id).eq("type",type));
        if(ToolUtil.isNotEmpty(list)){
            return list.get(0).getName();
        }
        return null;
    }

    @Override
    public List<AudienceCode> getBusinessList() {
        return audienceCodeDao.businessTypeList();
    }

    @Override
    public List<AudienceCode> getIndustryList() {
        return audienceCodeDao.industryTypeList();
    }

    @Override
    public List<AudienceCode> getPropertyList() {
        return audienceCodeDao.propertyList();
    }
    /**
     * @Auther:Victor
     * @Description: 用户上传的图片集合
     * @Date:Create in 15:58 2018/4/21
     * @Modified By:
     */
    @Override
    public List<UsersImages> getUsersImageList(Integer userid) {
        return usersImagesMapper.selectList(new EntityWrapper<UsersImages>().eq("userid", userid).orderBy("createtime", false));
    }
    /**
     * 根据用户id获取用户名称
     */
    @Override
    public String getUserNameById(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            return user.getName();
        } else {
            return "-";
        }
    }

    /**
     * 根据用户id获取用户账号
     */
    @Override
    public String getUserAccountById(Integer userId) {
        User user = userMapper.selectById(userId);
        if (user != null) {
            return user.getAccount();
        } else {
            return "--";
        }
    }

    /**
     * 通过角色ids获取角色名称
     */
    @Override
    public String getRoleName(String roleIds) {
        Integer[] roles = Convert.toIntArray(roleIds);
        StringBuilder sb = new StringBuilder();
        for (int role : roles) {
            Role roleObj = roleMapper.selectById(role);
            if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
                sb.append(roleObj.getName()).append(",");
            }
        }
        return StrKit.removeSuffix(sb.toString(), ",");
    }

    /**
     * 通过角色id获取角色名称
     */
    @Override
    public String getSingleRoleName(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getName();
        }
        return "";
    }

    /**
     * 通过角色id获取角色英文名称
     */
    @Override
    public String getSingleRoleTip(Integer roleId) {
        if (0 == roleId) {
            return "--";
        }
        Role roleObj = roleMapper.selectById(roleId);
        if (ToolUtil.isNotEmpty(roleObj) && ToolUtil.isNotEmpty(roleObj.getName())) {
            return roleObj.getTips();
        }
        return "";
    }



    /**
     * 获取菜单的名称们(多个)
     */
    @Override
    public String getMenuNames(String menuIds) {
        Integer[] menus = Convert.toIntArray(menuIds);
        StringBuilder sb = new StringBuilder();
        for (int menu : menus) {
            Menu menuObj = menuMapper.selectById(menu);
            if (ToolUtil.isNotEmpty(menuObj) && ToolUtil.isNotEmpty(menuObj.getName())) {
                sb.append(menuObj.getName()).append(",");
            }
        }
        return StrKit.removeSuffix(sb.toString(), ",");
    }

    /**
     * 获取菜单名称
     */
    @Override
    public String getMenuName(Integer menuId) {
        if (ToolUtil.isEmpty(menuId)) {
            return "";
        } else {
            Menu menu = menuMapper.selectById(menuId);
            if (menu == null) {
                return "";
            } else {
                return menu.getName();
            }
        }
    }

    /**
     * 获取菜单名称通过编号
     */
    @Override
    public String getMenuNameByCode(String code) {
        if (ToolUtil.isEmpty(code)) {
            return "";
        } else {
            if (Magic.STR_0.equals(code)) {
                return Magic.TOP_LEVEL;
            }
            Menu param = new Menu();
            param.setCode(code);
            Menu menu = menuMapper.selectOne(param);
            if (menu == null) {
                return "";
            } else {
                return menu.getName();
            }
        }
    }

    /**
     * 获取商业性质名称通过编号
     */
    @Override
    public String getAudienceNameByCode(String code) {
        if (ToolUtil.isEmpty(code)) {
            return "";
        } else {
            AudienceCode params = new AudienceCode();
            params.setCode(code);
            AudienceCode audienceCode = audienceCodeMapper.selectOne(params);
            if (audienceCode == null) {
                return "";
            } else {
                return audienceCode.getName();
            }
        }
    }

    /**
     * 获取性别名称
     */
    @Override
    public String getSexName(Integer sex) {
        return Sex.valueOf(sex);
    }

    /**
     * 获取用户登录状态
     */
    @Override
    public String getStatusName(Integer status) {
        return ManagerStatus.valueOf(status);
    }

    /**
     * 获取菜单状态
     */
    @Override
    public String getMenuStatusName(Integer status) {
        return MenuStatus.valueOf(status);
    }

    /**
     * 获取被缓存的对象(用户删除业务)
     */
    @Override
    public String getCacheObject(String para) {
        return LogObjectHolder.me().get().toString();
    }

    /**
     * 获取最后登入时间
     */
    @Override
    public String getLastLoginTime(Integer userId) {
        // TODO Auto-generated method stub
        Wrapper<LoginLog> wrapper = new EntityWrapper<>();
        wrapper = wrapper.eq("user_id", userId).orderBy("createtime", false);
        List<LoginLog> list = this.loginLogMapper.selectList(wrapper);
        if (ToolUtil.isNotEmpty(list)) {
            Date time = list.get(0).getCreatetime();
            return DateUtil.formatDate(time, "yyyy年MM月dd日 HH:mm:ss");
        }
        return "";
    }

    /**
     * 根据用户id获取用户
     */
    @Override
    public User getUserById(Integer userId) {
        // TODO Auto-generated method stub
        return userMapper.selectById(userId);
    }

    /**
     * 获取除 管理员 与超级管理员外所有用户
     *
     * @return
     */
    @Override
    public List<User> getUserIsNotAdmin() {
        List<Integer> roleidList = new ArrayList();
        roleidList.add(Const.ADMINISTRATOR_ROLE_ID);
        roleidList.add(Const.ADMIN_ROLE_ID);
        Wrapper<User> wrapper = new EntityWrapper();
        wrapper.notIn("roleid", roleidList);
        return userMapper.selectList(wrapper);
    }
}
